<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="icon" th:href="@{/img/logo.ico}" href="../static/img/logo.ico" type="images/x-ico"/>

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="../static/css/bootstrap.min.css" th:href="@{/css/bootstrap.min.css}">
    <title>班级列表</title>
    <link rel="stylesheet" th:href="@{/fontawesome/css/font-awesome.min.css}"
          href="../../static/fontawesome/css/font-awesome.min.css">

    <link rel="stylesheet"
          href="https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,500;0,600;0,700;1,400&display=swap">

    <!-- Fontawesome CSS -->
    <link rel="stylesheet" href="../static/fontawesome/css/fontawesome.min.css"
          th:href="@{/fontawesome/css/fontawesome.min.css}">
    <link rel="stylesheet" href="../static/fontawesome/css/all.min.css" th:href="@{/fontawesome/css/all.min.css}">

    <!-- Main CSS -->
    <link rel="stylesheet" href="../static/css/back_index.css" th:href="@{/css/back_index.css}">
    <style type="text/css">
        th, td {
            white-space: nowrap;
            overflow: hidden;
            word-break: keep-all;
        }
    </style>
</head>
<body>
<div class="main-wrapper">
    <div th:replace="commons/bar::#menubar(activeUri1='班级管理',activeUri2='班级列表')"></div>

    <div class="page-wrapper">
        <div class="content container-fluid">
            <div class="row">
                <div class="col-lg-12">
                    <h1>班级信息管理</h1>
                    <hr>
                    <div class="form-inline col-sm-12">
                        <label class="control-label col-sm-1" for="selectclassid"><strong>班号</strong></label>
                        <input type="text" id="selectclassid" class="form-control form-control-sm col-sm-3">
                        <label class="control-label col-sm-1" for="selectclassname"><strong>姓名</strong></label>
                        <input type="text" id="selectclassname" class="form-control form-control-sm col-sm-3">
                    </div>
                    <hr>
                    <div class="fixed-table-toolbar">
                        <div class="bs-bars pull-left">
                            <div id="toolbar" class="btn-group">
                                <button type="button" class="btn btn-primary btn-sm" data-toggle="modal"
                                        data-target="#importStudentModal">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 导入班级
                                </button>
                                <button type="button" class="btn btn-info btn-sm" data-toggle="modal"
                                        data-target="#addClassModal" id="addStudent">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 添加班级
                                </button>
                                <button type="submit" class="btn btn-danger btn-sm" data-toggle="modal"
                                        id="deleteCheckBtn" disabled="disabled">
                                    <span class="fa fa-plus" aria-hidden="true"></span> 删除选中
                                </button>
                            </div>
                        </div>
                        <div class="columns columns-right btn-group pull-right">
                            <div class="keep-open btn-group" title="列">
                                <button class="btn btn-default" type="button" name="refresh" title="刷新"
                                        onclick="myrefresh()">
                                    <i class="fa fa-refresh"></i></button>
                                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                    <i class="fas fa-table"></i><span class="caret"></span>
                                </button>
                                <ul class="dropdown-menu" role="menu">
                                    <li>
                                        <input type="checkbox" checked="checked" id="td1" onclick="hiddenTD('td1')">ID
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td2" onclick="hiddenTD('td2')">名称
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td3" onclick="hiddenTD('td3')">人数
                                    </li>
                                    <li>
                                        <input type="checkbox" checked="checked" id="td4" onclick="hiddenTD('td4')">操作
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                    <div id="addClassModal" class="modal fade" tabindex="-1" role="dialog"
                         aria-labelledby="myLargeModalLabel"
                         data-backdrop="static" data-keyboard="false">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <input type="hidden" value="0" id="addAmount">
                                <div class="modal-header">
                                    <h4 class="modal-title">添加班级</h4>
                                    <button type="button" class="close" data-dismiss="modal"
                                            aria-label="Close"><span
                                            aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <table align="center" class="table">
                                        <tr>
                                            <td>ID</td>
                                            <td>
                                                <input type="text" id="addClassId">
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>名称</td>
                                            <td>
                                                <input type="text" id="addClassName">
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <button id="submitAdd" type="button" class="btn btn-primary">添加</button>
                                    <button type="button" class="btn btn btn-secondary" data-dismiss="modal">关闭</button>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="fixed-table-container table-responsive">
                        <table class="table table-hover table-striped table-bordered" id="datas">
                            <thead>
                            <th style="max-width: 10px"><input type="checkbox" class="checkall"
                                                               onclick="mydeleteallcheck(this.checked)"></th>
                            <th class="td1">ID</th>
                            <th class="td2">名称</th>
                            <th class="td3">人数</th>
                            <th class="td4">操作</th>
                            </thead>
                            <tbody id="mytbody">
                            <tr id="template">
                                <td class="td0"></td>
                                <td class="td1"></td>
                                <td class="td2"></td>
                                <td class="td3"></td>
                                <td class="td4"></td>
                            </tr>
                            </tbody>
                        </table>
                        <br/>
                        <div>
                            <div class="pull-left">
                                <input type="hidden" id="offset" value="0">
                                显示第<span id="begin"></span>到第<span id="end"></span>条记录，总共<span id="sum"></span>条记录。
                                每页显示
                                <select id="limit" onchange="this.allClassIds = changeLimit(this.allClassIds)">
                                    <option value="5">5</option>
                                    <option value="10" selected="selected">10</option>
                                    <option value="15">15</option>
                                    <option value="30">30</option>
                                    <option value="50">50</option>
                                    <option value="100">100</option>
                                </select> 条记录
                            </div>
                            <div class="pull-right">
                                <nav aria-label="Page navigation">
                                    <ul class="pagination" id="pages"></ul>
                                </nav>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal fade" id="viewClassModal" tabindex="-1"
                     role="dialog" aria-labelledby="classModalLabel" aria-hidden="true">
                    <div class="modal-dialog">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title"
                                    id="classModalLabel">详细信息</h5>
                                <input type="hidden" id="classid">
                                <button type="button" class="close" data-dismiss="modal"
                                        aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table class="table table-hover table-striped table-bordered" align="center"
                                       id="modal-datas"></table>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-danger" id="submitDelete">删除班级</button>
                                <button type="button" class="btn btn-secondary"
                                        data-dismiss="modal">关闭
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
                <footer>
                    <p>2020 &copy; 软件学院学生信息管理系统 by 郑焙元</p>
                </footer>
            </div>
        </div>
    </div>
</div>
<script src="../static/js/jquery-3.5.1.min.js" th:src="@{/js/jquery-3.5.1.min.js}"></script>
<script>
    var allClassIds = [];

    var limit;
    var begin = 1;
    var end = 0;
    var pageNum;
    var currentPage = 1;

    $(function () {
        limit = $("#limit option:selected").val();
        TableInit();
        var buttonGroup = new ButtonGroup();
        buttonGroup.init();
    });

    var TableInit = function () {
        sendAjax('/admin/api/class', 'get', {
            limit: $("#limit option:selected").val(),
            offset: $("#offset").val(),
            classid: $("#selectclassid").val() === "" ? "-1" : $("#selectclassid").val(),
            classname: $("#selectclassname").val() === "" ? "-1" : $("#selectclassname").val(),
        }, function (res) {
            paging(res);
            $.each(res.data, function (i, c) {
                var row = $("#template").clone();
                row.find(".td0").html('<input type="checkbox" class="checks" onclick="mydeletecheck(' + c.classid + ')">');
                row.find(".td1").text(c.classid);
                row.find(".td2").text(c.classname);
                row.find(".td3").text(c.amount);
                let html = '<button type="button" class="btn btn-info btn-sm" data-toggle="modal" ' +
                    'data-target="#viewClassModal" onclick="viewClass(\'' + c.classid + '\')">详情</button>';
                row.find(".td4").html(html);
                row.appendTo("#datas");//添加到模板的容器中
            });
            $("#template:first").remove();
        }, '');
    }

    function myrefresh() {
        $("#mytbody").children().remove();
        $("#mytbody").html('<tr id="template">\n' +
            '                                <td class="td0"></td>\n' +
            '                                <td class="td1"></td>\n' +
            '                                <td class="td2"></td>\n' +
            '                                <td class="td3"></td>\n' +
            '                                <td class="td4"></td>\n' +
            '                            </tr>');
        TableInit();
    }

    function viewClass(classid) {
        $("#classid").val(classid);
        $("#modal-datas").html(' <thead>\n' +
            '                                    <th>学号</th>\n' +
            '                                    <th>姓名</th>\n' +
            '                                    </thead>\n' +
            '                                    <tbody id="modal-tobody">\n' +
            '                                    <tr id="modal-tr">\n' +
            '                                        <td class="td1"></td>\n' +
            '                                        <td class="td2"></td>\n' +
            '                                    </tr>\n' +
            '                                    </tbody>');
        sendAjax('/admin/api/class/students/' + classid, 'get', {}, function (res) {
            $.each(res.data, function (i, student) {
                var row = $("#modal-tr").clone();
                row.find(".td1").text(student.studentid);
                row.find(".td2").text(student.studentname);
                row.appendTo("#modal-datas");
            });
            $("#modal-tr:first").remove();
        }, '');
    }

    var ButtonGroup = function () {
        var object = {};


        object.init = function () {

            $('#submitAdd').click(function () {
                if (confirm('确认新增班级？')) {
                    sendAjax('/admin/api/class', 'post', {
                        amount: $("#addAmount").val(),
                        classid: $("#addClassId").val(),
                        classname: $("#addClassName").val(),
                    }, function (res) {
                        alert("新增成功！");
                        $('#addClassModal').modal('hide');
                        myrefresh();
                    }, "#submitAdd");
                }
            });

            $('#submitDelete').click(function () {
                if (confirm('确认删除班级？')) {
                    if (confirm('删除班级会删除班内所有学生，确定删除？')) {
                        sendAjax('/admin/api/class/' + $("#classid").val(), 'delete', {}, function (res) {
                            alert("删除成功！");
                            $('#viewClassModal').modal('hide');
                            myrefresh();
                        }, "#submitDelete");
                    }
                }
            });

            $('#deleteCheckBtn').click(function () {
                if (confirm('确认删除选中班级？')) {
                    if (confirm('删除班级会删除班内所有学生，确定删除？')) {
                        sendAjax('/admin/api/class/list', 'delete', {
                            allClassIds: JSON.stringify(allClassIds),
                        }, function (res) {
                            alert("删除成功！");
                            allClassIds = [];
                            $(".checkall").prop("checked", false);
                            $("#deleteCheckBtn").prop("disabled", "disabled");
                            myrefresh();
                        }, "#deleteCheckBtn");
                    }
                }
            });

        };

        $('#addStudent').click(function () {
            sendAjax("/admin/api/class", "get", {}, function (res) {
                var html = "";
                $.each(res.data, function (i, data) {
                    html += '<option value="' + data.classid + '">' + data.classname + '</option>';
                });
                $("#addStudentClassId").html(html);
            }, "");
        });

        $("#submitResetPassword").click(function () {
            sendAjax("/admin/api/student/" + $("#studentId").val(), 'put', {}, function (res) {
                alert("重置密码成功");
                $('#submitResetPassword').modal('hide');
                myrefresh();
            }, "#submitResetPassword");
        });

        return object;
    };

    function mydeleteallcheck(classid) {
        this.allClassIds = deleteallcheck(classid, allClassIds);
        console.log(allClassIds)
    }

    function mydeletecheck(classid) {
        this.allClassIds = deletecheck(classid, allClassIds);
        console.log(allClassIds)
    }

</script>
<script src="../static/js/popper.min.js" th:src="@{/js/popper.min.js}"></script>
<script src="../static/js/bootstrap.min.js" th:src="@{/js/bootstrap.min.js}"></script>

<!-- Slimscroll JS -->
<script src="../static/js/jquery.slimscroll.min.js" th:src="@{/js/jquery.slimscroll.min.js}"></script>

<!-- Custom JS -->
<script src="../static/js/script.js" th:src="@{/js/script.js}"></script>

</body>
</html>
